package es.unex.meigas.ww.core;

import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Geometry;

import es.unex.meigas.dataObjects.FeatureImpl;
import es.unex.meigas.dataObjects.IFeature;
import es.unex.meigas.dataObjects.IFeatureIterator;
import es.unex.meigas.exceptions.IteratorException;

public class WWFeatureIterator implements IFeatureIterator {

	private FeatureIterator<SimpleFeature> m_Iter;

    public WWFeatureIterator(FeatureCollection<SimpleFeatureType, SimpleFeature> fc){

    	m_Iter = fc.features();

    }

    public boolean hasNext() {

    	if (m_Iter != null){
    		return m_Iter.hasNext();
    	}
    	else{
    		return false;
    	}

    }

    public IFeature next() throws IteratorException{

    	if (m_Iter != null){
        	if (!m_Iter.hasNext()){
        		throw new IteratorException();
        	}
    		SimpleFeature gtFeat = m_Iter.next();
    		Object values[] = new Object[gtFeat.getAttributeCount() - 1];
    		for (int i = 1; i < gtFeat.getAttributeCount(); i++) {
    			values[i - 1] = gtFeat.getAttribute(i);
			}
    		IFeature feat = new FeatureImpl((Geometry) gtFeat.getDefaultGeometry(), values);
    		return feat;
    	}
    	else{
    		throw new IteratorException();
    	}

    }

	public void close() {

		m_Iter.close();

	}


}
